﻿package $${Obj.PackageName}$$.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.inspur.system.repository.Page;
import com.inspur.system.tools.file.Col;
import com.inspur.system.tools.file.ExcelExport;
import $${Obj.PackageName}$$.dao.$${Obj.ClassNameFirstUpper}$$Dao;
import $${Obj.PackageName}$$.model.$${Obj.ClassNameFirstUpper}$$;
import $${Obj.PackageName}$$.service.I$${Obj.ClassNameFirstUpper}$$Service;

@WebService
@Service("$${Obj.PackageName}$$.service.$${Obj.ClassNameFirstUpper}$$Service")
@Transactional(readOnly = true)
public class $${Obj.ClassNameFirstUpper}$$Service implements I$${Obj.ClassNameFirstUpper}$$Service {

	@Autowired
	private $${Obj.ClassNameFirstUpper}$$Dao $${Obj.ClassNameFirstLower}$$Dao;
	
	public $${Obj.ClassNameFirstUpper}$$ findOne(long id){
		return $${Obj.ClassNameFirstLower}$$Dao.findOne(id);
	}
	
	public List<$${Obj.ClassNameFirstUpper}$$> findAll(){
		return $${Obj.ClassNameFirstLower}$$Dao.findAll();
	}
	
	public List<$${Obj.ClassNameFirstUpper}$$> findBy(Map<String, String> parameters){
		return $${Obj.ClassNameFirstLower}$$Dao.findPage(parameters);
	}
	
	public Page<$${Obj.ClassNameFirstUpper}$$> findPageByCondition(
			Page<$${Obj.ClassNameFirstUpper}$$> page, Map<String, String> conditions,
			Map<String, String> orders) {
		long count = $${Obj.ClassNameFirstLower}$$Dao.getCount(conditions);

		String sort = "";
		if (orders != null) {
			for (Map.Entry<String, String> kv : orders.entrySet()) {
				sort += kv.getKey();
				sort += " ";
				sort += kv.getValue();
				sort += ",";
			}
			if (sort.endsWith(","))
				sort = sort.substring(0, sort.length() - 1);
		}

		if (conditions != null) {
			conditions.put("sort", sort);
		}

		List<$${Obj.ClassNameFirstUpper}$$> rows = $${Obj.ClassNameFirstLower}$$Dao.findPage(
				conditions, new RowBounds(page.getStart(), page.getLimit()));
		page.setRows(rows);
		page.setTotalCount(count);
		return page;
	}

	public String exportByCondition(String filetype,
			Map<String, String> conditions) {
		List<$${Obj.ClassNameFirstUpper}$$> result = $${Obj.ClassNameFirstLower}$$Dao.findPage(conditions);
		ExcelExport<$${Obj.ClassNameFirstUpper}$$> excelExport = new ExcelExport<$${Obj.ClassNameFirstUpper}$$>();
		List<Col> listCol = new ArrayList<Col>();
		##{FIELD
			listCol.add(new Col("$${ObjField.JsFieldNameFirstLower}$$", "$${ObjField.JsFieldNameFirstLower}$$"));
		}##
		String fileName = "数据表列表";
		String sheetName = "列表";
		String filePath = "";
		try {
			filePath = excelExport.createExcel("download", fileName, sheetName,
					listCol, result);
		} catch (Exception e) {
			e.printStackTrace();
			filePath = "生成Excel错误!";
		}
		return filePath;
	}	
	
	##{SQL
$$<(ObjSql.SqlType=insert)
	@Transactional(readOnly = false)
	public void create($${Obj.ClassNameFirstUpper}$$ $${Obj.ClassNameFirstLower}$$) {
		$${Obj.ClassNameFirstLower}$$Dao.create($${Obj.ClassNameFirstLower}$$);
	}
	>$$
$$<(ObjSql.SqlType=update)
	@Transactional(readOnly = false)
	public void update($${Obj.ClassNameFirstUpper}$$ $${Obj.ClassNameFirstLower}$$) {
		$${Obj.ClassNameFirstLower}$$Dao.update($${Obj.ClassNameFirstLower}$$);
	}
	>$$
$$<(ObjSql.SqlType=delete)
	@Transactional(readOnly = false)
	public void delete(Long id) {
		$${Obj.ClassNameFirstLower}$$Dao.delete(id);
	}
	>$$
}##
}